<template>
  <div class="wrapper" :class="{error}">
    <input 
    type="text" 
    :value="value" 
    :disabled="disabled" 
    :readonly="readonly"  
    @change="handlChange"
    @input="handleInput"
    @focus="handleFocus"
    @blur="handleBlur"
    >
    <template v-if="error">
      <icon  name="error"></icon>
      <span class="errormsg">{{error}}</span>
    </template>
  </div>
</template>

<script>
import Icon from './icon'
export default {
  name: 'gulu-input',
  components: {
    Icon
  },
  props: {
    value: {
      type: String
    },
    disabled: {
      type: Boolean,
      default: false
    },
    readonly: {
      type: Boolean,
      default: false
    },
    error: {
      type: String
    }
  },
  methods: {
    handlChange(e){
      this.$emit('change', e)
    },
    handleInput(e){
      this.$emit('input', e.target.value)
    },
    handleFocus(e){
      this.$emit('focus', e)
    },
    handleBlur(e){
      this.$emit('blur', e)
    }
  }
}
</script>

<style lang="scss" scoped>
$height: 32px;
$border-color: #999;
$border-color-hover: #666;
$border-radius: 4px;
$font-size: 12px;
$box-shadow-color: rgba(0,0,0,0.5);
$red: #f1453d;
.wrapper{
  font-size: $font-size;
  display: inline-flex;
  align-items: center;
  > *:not(:last-child) {
    margin-right: .5em;
  }
  > input{
    height: 32px;
    border: 1px solid $border-color;
    border-radius: $border-radius;
    padding: 0 8px;
    font-size: inherit;
    &:hover{
      border-color: $border-color-hover;
    }
    &:focus{
      box-shadow: inset 0 1px 3px $box-shadow-color;
      outline: none;
    }
    &[disabled], &[readonly]{
      border-color: #bbb;
      color: #bbb;
      cursor: not-allowed;
    }
  }
  &.error{
    > input{
      border-color: $red;
    }
  }
  .errormsg{
    color: red;
  }
}
</style>
